package com.fahai.web.controller;

import com.fahai.common.PageVo;
import com.fahai.utils.json.JsonResult;
import com.fahai.web.entity.MpayflowVO;
import com.fahai.web.entity.QbyeVO;
import com.fahai.web.entity.UserInfo;
import com.fahai.web.service.UserInfoService;
import com.fahai.web.vo.ClientManageConditionVo;
import com.fahai.web.vo.UserInfoListVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 客户管理
 */

@Controller
@EnableGlobalMethodSecurity(prePostEnabled = true)
@RequestMapping(value = "/user")
public class ClientManagerController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(ClientManagerController.class);

    @Autowired
    private UserInfoService service;
    /**
     * 用户分页条件查询
     *
     * @param pageVo
     * @param model
     * @return
     * @throws IOException
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0','客户管理-1')")
    @ResponseBody
    @RequestMapping(value = "/userlist", method = RequestMethod.POST)
    public JsonResult userinfo(PageVo pageVo, Model model, ClientManageConditionVo userInfo) throws IOException {
        pageVo = service.getClientList(userInfo, pageVo);
        return ok("查询", pageVo);
    }

    /**
     * 分页总数
     *
     * @param model
     * @return
     * @throws IOException
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0','客户管理-1')")
    @ResponseBody
    @RequestMapping(value = "/listcount", method = RequestMethod.POST)
    public JsonResult listcount(PageVo pageVo, Model model, ClientManageConditionVo userInfo) throws IOException {
                int count= service.getClientCount(userInfo, pageVo);
        pageVo.setTotal(  Long.valueOf( count));
        model.addAttribute("count", pageVo);
        model.addAttribute("selectBar", "left_user");
        return ok("查询", pageVo.getTotal());
    }
    /**
     * 查看要编辑啊的用户简明信息
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/showqbye", method = RequestMethod.POST)
    public JsonResult showqbye(PageVo pageVo, UserInfo userInfo) {
        List<QbyeVO> qbyeVOS= service.getWalletBalance(userInfo.getUid());//
         pageVo.setResult(qbyeVOS);
        return ok("查询", pageVo.getResult());
    }
   /**
     * 查看M站消费记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/mpayflow", method = RequestMethod.POST)
    public JsonResult mpayflow(PageVo pageVo, UserInfo userInfo) {
        List<MpayflowVO> mpayflowVOS= service.getMpayFlow(userInfo.getUid());//
         pageVo.setResult(mpayflowVOS);
        return ok("查询", pageVo);
    }
    /**
     * 查看M站消费记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/wxbody", method = RequestMethod.POST)
    public JsonResult wxbody(PageVo pageVo, UserInfo userInfo) {
        List<MpayflowVO> mpayflowVOS= service.getWxbody(userInfo.getUid());//
         pageVo.setResult(mpayflowVOS);
        return ok("查询", pageVo);
    }
   /**
     * 查看M充值记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/paymz", method = RequestMethod.POST)
    public JsonResult paymz(PageVo pageVo, UserInfo userInfo) {
        List<MpayflowVO> mpayflowVOS= service.getPaymz(userInfo.getUid());//
         pageVo.setResult(mpayflowVOS);
        return ok("查询", pageVo);
    }
   /**
     * 查看公众号充值记录
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/paywx", method = RequestMethod.POST)
    public JsonResult paywx(PageVo pageVo, UserInfo userInfo) {
        List<MpayflowVO> mpayflowVOS= service.getPaywx(userInfo.getUid());//
         pageVo.setResult(mpayflowVOS);
        return ok("查询", pageVo);
    }

    /**
     * 跳转到查询页面
     *
     * @param
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0','客户管理-1')")
    @RequestMapping(value = "/userToSelect", method = RequestMethod.GET)
    public String userToSelect(Model model) {
        model.addAttribute("selectTab","客户管理");
        return "client_manage";//新界面
    }


    /**
     * 查看要编辑啊的用户简明信息
     *
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0','客户管理-1')")
    @RequestMapping(value = "/showDetail", method = RequestMethod.GET)
    public String userDetail(Model model, Integer uid) {
        if(null==uid){
            return "client_detail";
        }
        UserInfoListVo userBaseInfo = service.getUserInfoListVoById(uid);//获取用户基本信息
        int  effectiveTimes=service.getEffectiveTimes(uid);//有效通话次数
        if(null!=userBaseInfo.getUname()&&!"".equals(userBaseInfo.getUname())) {
            try {
                userBaseInfo.setUname(URLDecoder.decode(userBaseInfo.getUname(), "utf-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        userBaseInfo.setBak2(String.valueOf(effectiveTimes));
        model.addAttribute("userBaseInfo", userBaseInfo);//用户基本信息列表
        model.addAttribute("selectTab","客户管理");
        return "client_detail";
    }



    /**
     * 打开通话记录
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0','客户管理-1')")
        @RequestMapping(value = "/findConverseListPage",method= RequestMethod.GET)
    public String findConverseListPage( Integer uid,Model model){
        model.addAttribute("userid",uid);
        model.addAttribute("selectTab","客户管理");
        return "converse_list_client";
    }



    /**
     * 查询通话记录
     * @return
     */
    @ResponseBody
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0','客户管理-1')")
    @RequestMapping(value = "/findClientRecordList",method= RequestMethod.POST)
    public JsonResult findConverseList(UserInfo userInfo, PageVo pageVo ,Model model){
        pageVo = service.findCallFlowList(userInfo,pageVo);
        model.addAttribute("selectTab","客户管理");
        return ok("通话流程表",pageVo);
    }


    /**
     * 编辑单个用户的简明信息
     *
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0')")
    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updateUserInfo(Model model, UserInfo userInfo) {
        service.updateUserInfo(userInfo);
        return ok("修改成功");
    }

    /**
     * 编辑用户备注
     *
     * @param model
     * @return
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0' )")
    @RequestMapping(value = "/updatereMark", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult updateUserNote(Model model, UserInfo userInfo) {
        service.updateRemark(userInfo);
        return ok("修改成功");
    }




    /**
     * 批量冻结账户
     *
     * @param
     * @return userfrozeAccount
     */
    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0' )")
    @RequestMapping(value = "/userfrozeAccount", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult userfrozeAccount(@RequestParam(required = false, value = "list[]") List<String> list) {

        List<Integer> idlist = new ArrayList<Integer>();
        for (String id : list
                ) {
            idlist.add(Integer.valueOf(id));
        }
        service.userfrozeAccount(idlist);
        return ok("冻结成功");
    }

    /**
     * 批量解冻账户
     *
     * @param
     * @return
     */

    @PreAuthorize("hasAnyAuthority('ADMIN','客户管理-0' )")
    @RequestMapping(value = "/userunfrozeAccount", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult userunfrozeAccount(@RequestParam(required = false, value = "list[]") List<String> list) {
        List<Integer> idlist = new ArrayList<Integer>();
        for (String id : list
                ) {
            idlist.add(Integer.valueOf(id));
        }
        service.userunfrozeAccount(idlist);
        return ok("解冻成功");
    }

}
